package com.woniu.defecttracking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.defecttracking.annotation.AddNum;
import com.woniu.defecttracking.commens.ComUtil;
import com.woniu.defecttracking.dto.BugInfosDto;
import com.woniu.defecttracking.dto.BugTypeDto;
import com.woniu.defecttracking.dto.FindBugDto;
import com.woniu.defecttracking.entity.BugType;
import com.woniu.defecttracking.mapper.BugTypeMapper;
import com.woniu.defecttracking.param.FindBugParam;
import com.woniu.defecttracking.service.BugTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tiangle
 * @since 2021-12-31
 */
@Service
public class BugTypeServiceImpl extends ServiceImpl<BugTypeMapper, BugType> implements BugTypeService {
    @Override
    public FindBugDto findBugInfo() {
        QueryWrapper<BugType> bugTypeQueryWrapper = new QueryWrapper<>();
        bugTypeQueryWrapper.eq("isdel",0);
        bugTypeQueryWrapper.eq("status",0);

        List<BugType> list = list(bugTypeQueryWrapper);
        FindBugDto findBugDto = new FindBugDto();
        findBugDto.setBugTypes(list);
        return findBugDto;
    }

    @AddNum("bugTypeNum")
    @Override
    public boolean addBugInfo(FindBugParam findBugParam) {
        BugType bugType = ComUtil.copyProperties(findBugParam, BugType.class);

        return save(bugType);
    }

    @Override
    public boolean updateBugInfo(FindBugParam updateBugParam) {
        BugType bugType = ComUtil.copyProperties(updateBugParam, BugType.class);

        return updateById(bugType);
    }

    @Override
    public boolean deleteBugInfo(List<Integer> ids) {

        for (Integer id : ids) {
            BugType byId = getById(id);
            if (byId.getIsdel()==1){
                return false;
            }
        }
        System.out.println(ids);
        List<BugType> bugTypes = listByIds(ids);
        bugTypes.forEach(a->a.setIsdel(1));

        return saveOrUpdateBatch(bugTypes);
    }

    @Override
    public boolean updateStatusUp(Integer id) {
        QueryWrapper<BugType> objectQueryWrapper = new QueryWrapper<>();
        BugType bugType = getOne(objectQueryWrapper.eq("bug_type_id",id));
        if (bugType.getStatus()==0){
            return false;
        }
        bugType.setStatus(0);
        if (updateById(bugType)){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateStatusDown(Integer id) {
        QueryWrapper<BugType> objectQueryWrapper = new QueryWrapper<>();
        BugType bugType = getOne(objectQueryWrapper.eq("bug_type_id",id));
        if (bugType.getStatus()==1){
            return false;
        }
        bugType.setStatus(1);
        if (updateById(bugType)){
            return true;
        }
        return false;
    }

    @Override
    public BugTypeDto findAllBugInfo() {
        QueryWrapper<BugType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("isdel",0);
        List<BugType> list = list(queryWrapper);


        BugTypeDto bugTypeDto = new BugTypeDto();
        bugTypeDto.setBugTypes(list);

        return bugTypeDto;
    }
}
